<template>
  <d2-container>
    <div class="mt-2 ml-2 flex-1">
      <div class="pb-2">
        <div><el-button type="warning" @click="addCategory" icon="el-icon-plus">添加分类</el-button></div>
      </div>
      <div class="flex-1">
        <el-table :data="categoryList" stripe style="width: 100%;">
          <el-table-column align="center" label="序号" type="index" width="65" />
          <el-table-column align="center" label="分类名称" prop="category_name" width="80" />
          <el-table-column align="center" label="分类封面" prop="cover_image" width="80">
            <template slot-scope="scope">
              <el-popover placement="right" trigger="hover">
                <el-image slot="reference" style="width: 40px; height: 40px" :src="scope.row.cover_image" fit="cover" />
                <el-image style="width: 220px; height: 220px" :src="scope.row.cover_image" fit="contain" />
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column align="center" label="分类icon" prop="icon" width="100" />
          <el-table-column align="center" label="上级分类" prop="parent_name" width="80" />
          <el-table-column align="center" label="添加时间" prop="create_time" width="80" />
          <el-table-column align="center" label="操作" width="130">
            <template slot-scope="scope">
              <el-button size="mini" @click="categoreEdit(scope.row.id)">修改</el-button>
              <el-button size="mini" type="danger" @click="openDelete(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="flex-1 mt-3">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page.sync="queryInfo.page" layout="total, prev, pager, next" :total="categoryTotal">
        </el-pagination>
      </div>
      <el-dialog :visible.sync="dialogVisible" width="40%" title="添加/编辑分类">
        <el-form :model="form" ref="form" label-width="80px">
          <el-form-item label="名称" prop="category_name">
            <div style="width: 50%;">
              <el-input v-model="form.category_name"></el-input>
            </div>
          </el-form-item>
          <el-form-item label="父分类" prop="parent">
            <div style="width: 50%;">
              <el-cascader v-model="form.parent" :props="{value:'id', label: 'category_name', checkStrictly: true}"
                :options="categoriesTree" @change="handleChange"></el-cascader>
            </div>
          </el-form-item>
          <el-form-item label="排序" prop="sort">
            <div style="width: 18%;">
              <el-input v-model="form.sort"></el-input>
            </div>
          </el-form-item>
          <el-form-item label="图标" prop="icon">
            <div style="width: 45%;">
              <el-input v-model="form.icon"></el-input>
            </div>
          </el-form-item>
          <el-form-item label="封面图片" prop="cover_image">
            <el-upload class="avatar-uploader" :action="actionUrl" :show-file-list="false" :on-success="handleSuccess">
              <img v-if="form.cover_image" :src="form.cover_image" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="关键词" prop="icon">
            <div style="width: 80%;">
              <el-input v-model="form.keywords" placeholder="多个关键词用英文逗号隔开"></el-input>
            </div>
          </el-form-item>
          <el-form-item label="分类简介" prop="description">
            <div style="width: 80%;">
              <el-input v-model="form.description" type="textarea" rows="4"></el-input>
            </div>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </d2-container>
</template>
<script>
  import {
    imagesUploadUrl,
  } from '@/utils/interface'
  import {
    forumcategorytreeUrl,
    forumcategoryListUrl,
    forumcategoryDetailUrl,
    forumcategoryCreateUrl,
    forumcategoryUpdateUrl,
    forumcategoryDeleteUrl
  } from "@/utils/forum"
  import {
    baseURL
  } from '@/utils/request'
  export default {
    data() {
      return {
        dialogVisible: false,
        addSatus: true,
        queryInfo: {
          page: 1,
          page_size: 10
        },
        categoryList: [],
        categoriesTree: [],
        categoryyDetail: '',
        actionUrl: baseURL + imagesUploadUrl,
        form: {
          category_name: '',
          parent: '',
          keywords: '',
          description: '',
          cover_image: '',
          icon: '',
          fost: 1
        },
        getCategoreId:'',
        categoryTotal:1
      }
    },
    created() {
      this.getForumcategory()
      this.getCategoriesTree()
    },
    methods: {
      getCategoriesTree() {
        this.$get(forumcategorytreeUrl, this.queryInfo).then(res => {
          console.log('777', res.data);
          this.categoriesTree = res.data.data
          console.log('6666', this.categoriesTree);
        })
      },
      getForumcategory() {
        this.$get(forumcategoryListUrl, this.queryInfo).then(res => {
          if (res.data.code == 200) {
            this.categoryList = res.data.data.list
            this.categoryTotal = res.data.data.total
          }
        })
      },
      // 分页页码显示条数
      handleSizeChange(newSize) {
        // 把每页显示多少数据重新赋值
        this.queryInfo.page_size = newSize
        this.getForumcategory()
      },
      // 翻页事件
      handleCurrentChange(newPage) {
        this.queryInfo.page = newPage
        this.getForumcategory()
      },
      handleChange(value) {
        if (value && value.length > 0) {
          this.form.parent = value[value.length - 1]; // 更新form.category的值为选中的最后一个值
        }
        console.log(this.form.parent);
      },
      categoreEdit(categoreId) {
        this.dialogVisible = true
        this.addSatus = false
        this.getCategoreId = categoreId
        this.$get(forumcategoryDetailUrl + categoreId + '/').then(res => {
          if (res.data.code == 200) {
            this.form = {
              category_name: res.data.data.category_name,
              parent: res.data.data.parent,
              keywords: res.data.data.keywords,
              description: res.data.data.description,
              cover_image: res.data.data.cover_image,
              icon: res.data.data.icon,
              fost: res.data.data.fost
            }
          }
        })
      },
      addCategory(){
        this.dialogVisible = true
        this.addSatus = true
      },
      handleSuccess(response, file, fileList) {
        this.form.cover_image = response.data.url
      },
      submitForm() {
        if(this.addSatus){
          this.$post(forumcategoryCreateUrl, this.form).then(res=>{
            if(res.data.code == 200){
              this.form = {}
              this.dialogVisible = false
              this.$message({
                type: 'success',
                message: '添加成功!'
              });
              this.getForumcategory()
            }
          })
        }else{
          this.$put(forumcategoryUpdateUrl + this.getCategoreId + '/', this.form).then(res=>{
            if(res.data.code == 200){
              this.form = {}
              this.dialogVisible = false
              this.$message({
                type: 'success',
                message: '添加成功!'
              });
              this.getForumcategory()
            }
          })
        }
      },
      openDelete(catagoryId) {
        this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 在确定时执行handleDelete的删除
          this.handleDelete(catagoryId);
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      handleDelete(categoryId) {
        this.$del(forumcategoryDeleteUrl + categoryId + '/').then(res => {
          this.getForumcategory()
        })
      },
    }
  }
</script>

<style>
</style>
